package com.cjxt.ams.shiro;

import com.cjxt.ams.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * @author XueYunFei
 * @version V1.0
 * @Description:
 * @className: ShiroUtil
 * @date 2021/11/22 16:45
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
public class ShiroUtils {

	/**  加密算法 */
//	public final static String hashAlgorithmName = "SHA-256";
//	/**  加密次数 */
//	public final static int hashIterations = 16;
//

//	public static String sha256(String password, String salt) {
//		return new SimpleHash(hashAlgorithmName, password, salt, hashIterations).toString();
//	}
	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}


	public static User getUser() {
		return (User)SecurityUtils.getSubject().getPrincipal();
	}

	public static Integer getUserId() {
		return getUser().getUserId();
	}

	public static void setSessionAttribute(Object key, Object value) {

		getSession().setAttribute(key, value);
	}
	public static Object getSessionAttribute(Object key) {
		return getSession().getAttribute(key);
	}

	public static boolean isLogin() {
		return SecurityUtils.getSubject().getPrincipal() != null;
	}
	/**
	 * @description: TODO
	 * @author XueYunfei
	 * @date  2021/11/22 17:47
	 */
	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	/**
	 * @description: 获取验证码
	 * @author XueYunfei
	 * @date  2021/11/22 17:10
	 @param key
	 */

	public static String getKaptcha(String key){
		Object kaptcha=getSessionAttribute(key);
		if(kaptcha==null){
			return "验证失败";
		}
		getSession().removeAttribute(key);
		return kaptcha.toString();
	}

	public static void logout() {
		SecurityUtils.getSubject().logout();
	}
}
